package com.googlecode.dgwt.client.dojo;

import com.google.gwt.core.client.GWT;


/**
 * @author	Wellington Tadeu dos Santos
 * @since	DGWT v0.01
 */
public class Console {


    public static void log(Object ...params){
        for (Object p : params){
            System.out.print(p);
        }
        
        System.out.println();
        
        if (GWT.isScript()) {
            _log(params);
        }
    }

    public static void warning(String message) {
        warning(null, message);
    }
    

    public static void warning(Object component, String message) {
        String name = "";
        if (component != null) {
            name = ": " + Dojo.reflect.getSimpleName(component.getClass());
        }
        System.out.println("<WARNING" + name + "> " + message);
        if (GWT.isScript()) {
            _warning(name + ": " + message);
        }
    }
    

    public static void error(String message) {
        error(null, message);
    }

    public static void error(Throwable e) {
        error(null, e);
    }

    public static void error(Object component, String message) {
        String name = "";
        if (component != null) {
            name = ": " + Dojo.reflect.getSimpleName(component.getClass());
        }
        
        System.err.println("<ERROR" + name + "> " + message);
        if (GWT.isScript()) {
            _error(name + ": " + message);
        }
    }
    
    public static void error(Object component, Throwable e) {
        error(component, e.getMessage());
        e.printStackTrace(System.err);
        if (GWT.isScript()) {
            _logStackTrace(e.getStackTrace());
        }
    }


    private static native void _logStackTrace(Object stackTrace) /*-{
        console.log({'stackTrace': stackTrace});
    }-*/;

    private static native void _log(Object[] params) /*-{
        console.log.apply(console.log, @com.googlecode.dgwt.client.dojo.core.JsArray::as([Ljava/lang/Object;)(params));
    }-*/;

    private static native void _warning(Object value) /*-{
        console.warn(value);
    }-*/;

    private static native void _error(Object value) /*-{
        console.error(value);
    }-*/;

}
